<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\ProductWish;
use app\admin\model\User;
use app\admin\model\ProductCategory;

/**
 * 心愿管理
 *
 * @icon   fa fa-list
 */
class Wish extends Backend
{

    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ProductWish();
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                //获取分类名称
                $category =  ProductCategory::where(['id'=>$v['category_id']])->find();
                //分类名称
                $list[$k]['category'] = $category['name'];
                $user =  User::where(['id'=>$v['user_id']])->find();
                $list[$k]['nickname'] = $user['nickname'];
                $imgs = explode(',',$v['video_img']);
                $list[$k]['video_img'] = $imgs[0];
                if(strstr($list[$k]['video_img'], 'mp4')){
                    $list[$k]['video_img'] =  $list[$k]['video_img'].'?x-oss-process=video/snapshot,t_1000,m_fast,ar_auto';
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    
    //上/下架
    public function setStatus()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['id']){
                $this->error('参数错误');
            }
            $result =  $this->model->where(['id'=>$data['id']])->update(['status'=>$data['status']]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
    }

    //精选
    public function setRecommendTime()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['type']){
                $this->error('参数错误');
            }
            if($data['type'] == 1){
                $recommend_time = time();
            }else{
                $recommend_time = 0;
            }
            $result = ProductWish::where(['id'=>$data['id']])->update(['recommend_time'=>$recommend_time]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['id']){
                $this->error('参数错误');
            }
            $result = ProductModel::where(['id'=>$data['id']])->update(['status'=>$data['status']]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->where('id',$ids)->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }



}
